package com.ericzong.renamer.converter.file;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import com.ericzong.renamer.converter.string.AddStringConverter;
import com.ericzong.renamer.model.Record;

public class AddPathConverter extends AddStringConverter
{

	@Override
	public void convert(Record record) {
		StringBuffer target = new StringBuffer(record.getSource());
		String baseName = FilenameUtils.getBaseName(record.getSource());
		int length = record.getSource().length();
		int baseLength = baseName.length();
		int extLength = length - baseLength;
		
		if(StringUtils.isNoneEmpty(getInsertString()))
		{
			target.insert(getIndex(getInsertIndex(), baseLength), getInsertString());
		}
		
		if(StringUtils.isNoneEmpty(getStartString()))
		{
			target.insert(0, getStartString());
		}
		
		if(StringUtils.isNoneEmpty(getEndString()))
		{
			target.insert(target.length() - extLength, getEndString());
		}
		
		record.setTarget(target.toString());
	}

}
